/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package password;

import amount.*;
import java.util.ArrayList;
import length.Length01;
import rule.Rule;
import score.DefaultScore;
import score.MinOccuranceScore;

/**
 *
 * @author Daniel Gribel
 */
public class PasswordChecker01 {
	
    public static final String VERY_WEAK = "very weak";
    public static final String WEAK = "weak";
    public static final String MEDIUM = "medium";
    public static final String STRONG = "strong";
    public static final String STRONGER = "stronger";
   
    public static final String[] classification = {VERY_WEAK, WEAK, MEDIUM, STRONG, STRONGER};
    public static final int[] interval = {16, 25, 35, 45, Integer.MAX_VALUE};
    
    private ArrayList<Rule> rules;
    private int checkerScore;
    private String result;
    private Length01 length;
    
    public PasswordChecker01() {
        super();
        this.rules = new ArrayList<Rule>();
        this.checkerScore = 0;
        appendRules();
    }
    
    private void appendRules() {
        length = new Length01(new DefaultScore());
        this.rules.add(new LowerCaseAmount(new MinOccuranceScore(1, 1), "at least 1 lowercase"));
        this.rules.add(new UpperCaseAmount(new MinOccuranceScore(1, 5), "at least 1 uppercase"));
        this.rules.add(new DigitAmount(new MinOccuranceScore(1, 5), "at least 1 digit"));
        this.rules.add(new DigitAmount(new MinOccuranceScore(3, 5), "at least 3 digits"));
        this.rules.add(new SpecialCharAmount(new MinOccuranceScore(1, 5), "at least 1 special char"));
        this.rules.add(new SpecialCharAmount(new MinOccuranceScore(2, 5), "at least 2 special chars"));
        this.rules.add(new UpperAndLowerCaseAmount(new MinOccuranceScore(1, 2), "upper and lowercase occurance (bonus)"));
        this.rules.add(new LetterAndDigitAmount(new MinOccuranceScore(1, 2), "letter and digit occurance (bonus)"));
        this.rules.add(new LetterOrDigitAndSpecialCharAmount(new MinOccuranceScore(1, 2), "alphanumeric and special char occurance (bonus)"));
    }
    
    public void checkPassword(String password) {
        length.calculate(password);
        checkerScore = length.getScore().getFinalScore();
        
        System.out.println("+" + length.getScore().getFinalScore() + ": length (" + length.getLength() + ")");
        
        for(Rule rule : this.rules) {
            rule.checkConditions(password);
            rule.applyScore();
            this.checkerScore = this.checkerScore + rule.getScore().getFinalScore();
            System.out.println("+" + rule.getScore().getFinalScore() + ": " + rule.getName());
        }
        
        result = classify();
        printResult();
    }
	
    private String classify() {
        for(int i = 0; i < classification.length; i++) {
            if(checkerScore < interval[i])
                return classification[i];
        }
        return STRONGER;
    }
   
    private void printResult() {
        System.out.println("Strength score: " + checkerScore);
        System.out.println("Strength verdict: " + result);
    }

    public int getCheckerScore() {
        return checkerScore;
    }

    public String getResult() {
        return result;
    }
	
}

